package com.hongdee.crm.tdc.mapper;

import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

/**
 * @author zhangkai
 * @desc 一句话描述
 **/
public class MarketQuestionOrderSQL {

    /****
     * 查询公司当前启用的问券参数,获取问券编码
     * @return
     */
    public String queryParamSQL() {
        return new SQL() {{
            SELECT("value");
            FROM(" t_bc_param_set ");
            WHERE(" company_id =#{companyId}");
            WHERE(" code ='srhf_code' and p_code='0201' ");

        }}.toString();
    }


    /***
     * 查询是否存在已启用的调研问券
     * @return
     */
    public String queryEnabelNarieSQL() {
        return new SQL() {{
            SELECT(" * ");
            FROM(" t_market_dc_question_naire ");
            WHERE(" company_id =#{companyId}");
            WHERE(" code = #{narieCode} ");

        }}.toString();
    }

    /***
     * 查询已挂入工单的调研问券
     * @return
     */
    public String queryOrderNumSQL() {
        return new SQL() {{
            SELECT("count(1) as score ");
            FROM(" t_market_dc_question_naire_order_info ");
            WHERE(" company_id =#{companyId}");
            WHERE(" order_num = #{orderNum} ");

        }}.toString();
    }

    /***
     * 绑定工单与调研问券
     * @param params
     * @return
     */
    public String bindOrderInfoSQL(Map params) {
        StringBuffer sq = new StringBuffer();
        sq.append("INSERT INTO t_market_dc_question_naire_order_info (id, narie_code, library_id, assgin_score, company_id " +
                ", label, label_type, value_type, p_id, order_num) " +
                "SELECT replace(uuid(), '-', ''), narie_code, library_id, assgin_score, company_id " +
                " , label, label_type, value_type, p_id, '" + params.get("orderNum").toString() + "' " +
                "FROM t_market_dc_question_naire_info " +
                "WHERE company_id = #{companyId}" +
                " AND `narie_code` = #{narieCode}");
        return sq.toString();
    }

    /***
     * 查询问券子项
     */
    public String queryItemSQL() {
        return new SQL() {{
            SELECT(" * ");
            FROM(" t_market_dc_question_naire_order_info ");
            WHERE(" company_id = #{companyId} ");
            WHERE(" order_num = #{orderNum} ");
            WHERE(" p_id = #{pId} ");
            ORDER_BY(" label ");
        }}.toString();
    }


    /***
     * 更新试券得分
     *
     */
    public String updateOrderItemScoreSQL() {
        return new SQL() {{

            UPDATE(" t_market_dc_question_naire_order_info ");
            SET(" score = #{score} ");
            WHERE(" id = #{id} ");

        }}.toString();
    }

    /****
     * 计算上级得分
     */
    public String countParentItemScoreSQL() {
        return new SQL() {{

            SELECT(" SUM(score) as score,t1.p_id,t1.order_num,t1.company_id ");
            FROM("  t_market_dc_question_naire_order_info t1 ");
            WHERE(" select 1 from t_market_dc_question_naire_order_info t2 where t2.id=#{id} and t1.order_num =t2.order_num and t1.p_id = t2.p_id ");

        }}.toString();
    }

    /***
     * 通过工单号，公司ID，LibaryID，更新分数
     * @return
     */
    public String updateOrderScoreByOrderNumSQL(){
        return new SQL() {{

            UPDATE(" t_market_dc_question_naire_order_info ");
            SET(" score = #{score} ");
            WHERE(" library_id = #{libraryId} ");
            WHERE(" order_num  = #{orderNum} ");
            WHERE(" company_Id  = #{companyId} ");


        }}.toString();
    }
}
